package com.modulethree.homework3;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

/*

 (2)定义Map集合，用Student对象作为key，用字符串(此表示表示学生的住址)作为value

 (3)利用四种方式遍历Map集合中的内容，格式：key:value

 作者：翁海龙
 */

public class StudentMap {


        public static void main(String[] args) {
            HashMap<Student, String> hm = new HashMap<Student,String>();
            String str1;
            str1=hm.put(new Student("小翁",18), "广州");
            str1=hm.put(new Student("小海",18), "深圳");
            str1=hm.put(new Student("小龙",18), "北京");
            str1=hm.put(new Student("小八",18), "上海");

            Method1(hm);
            Method2(hm);
        }

        public static void Method1(HashMap<Student, String> str1) {

            Set<Map.Entry<Student, String>> studentSet = str1.entrySet();

            System.out.println("第一种方式遍历：调用str1集合方法entrySet方法,用Iterator迭代取出key:");
            Iterator<Map.Entry<Student, String>> studentIt = studentSet.iterator();
            while(studentIt.hasNext()) {
                Map.Entry<Student, String> entry = studentIt.next();
                Student studentKey = entry.getKey();
                String value = entry.getValue();
                System.out.println("\t"+studentKey+"的地址："+value);
            }

            System.out.println("\n第二种方式遍历：调用str1集合方法：entrySet方法,用for···each循环取出key:");
            for(Map.Entry<Student, String> me : str1.entrySet()) {
                Student studentKey = me.getKey();
                String value = me.getValue();
                System.out.println("\t"+studentKey+"的地址："+value);
            }

        }

        public static void Method2(HashMap<Student, String> str1) {

            Set<Student> studentSet = str1.keySet();
            System.out.println("\n第三种方式遍历：调用str1集合方法：keySet方法,用Iterator迭代取出key:");
            Iterator<Student> studentIt = studentSet.iterator();
            while(studentIt.hasNext()) {
                Student studentKey = studentIt.next();
                String value = str1.get(studentKey);
                System.out.println("\t"+studentKey+"的地址："+value);
            }

            System.out.println("\n第四种方式遍历：调用str1集合方法：keySet方法,用for···each循环取出key:");

            for(Student stuSet : str1.keySet()) {
                Student studentKey = stuSet;
                String value = str1.get(studentKey);
                System.out.println("\t"+studentKey+"的地址："+value);
            }
        }
}
